package org.testing.model.correctQuestionAnswers;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.testing.model.userAnswers.UserAnswer;

public class CorrectMultiAnswer implements CorrectAnswer {
	private String[] text;

	public CorrectMultiAnswer(String[] answer) {
		this.text = answer;
	}
	
	@Override
	public double equals(UserAnswer userUnswer) {
		List<String> listCorrectAnswers = new ArrayList<String>(Arrays.asList(this.text));
		List<String> listUserAnswers = new ArrayList<String>(Arrays.asList(userUnswer.getAnswers()));
		
		
		
		int startCorrectLength = listCorrectAnswers.size();
		Iterator<String> userAnswerIterator = listUserAnswers.iterator();
		Iterator<String> correctAnswerIterator = listCorrectAnswers.iterator();
		
		while(correctAnswerIterator.hasNext()) {
			String commonCorrectAnswer = correctAnswerIterator.next();
			while(userAnswerIterator.hasNext()) {
				String commonUserAnswer = userAnswerIterator.next();
				if(commonCorrectAnswer.equals(commonUserAnswer)) {
					correctAnswerIterator.remove();	
					break;
				}
			}
		}
		return (double) (startCorrectLength-listCorrectAnswers.size())/startCorrectLength;
	}
}


